import { mapState, mapGetters } from 'vuex'
import util from 'service/util'

export default {
    data() {
        return {
            /**
             * signSuccess: 签约成功
             * signFail: 签约失败
             * sesameSuccess: 芝麻信用成功
             * sesameFail: 芝麻信用失败
             */
            configs: {
                signSuccess: {
                    imgType: 'S',
                    text: '签约已受理',
                    path: '/loan/action'
                },
                signFail: {
                    imgType: 'F',
                    text: '签约失败',
                    path: '/loan/action'
                },
                sesameSuccess: {
                    imgType: 'S',
                    text: '认证成功',
                    path: '/loan/cacenter'
                },
                sesameFail: {
                    imgType: 'F',
                    text: '认证失败',
                    path: '/loan/cacenter'
                },
                logOff: {
                    imgType: 'N',
                    text: '用户未登录',
                    path: '/user/login'
                }
            },
            selectedConfig: null
        }
    },
    computed: {
        ...mapState(['user']),
        ...mapGetters(['wxReady', 'userLogined'])
    },
    created() {
        this.type = this.$route.query.type
        this.selectedConfig = this.configs[this.type]
        if (this.selectedConfig == null) {
            $router.push('/system/neterror')
        }
        // 如果不是微信，通过原生获取lbs信息
        if (!util.platform().wechat && this.userLogined && window.hybrid == null && this.type == 'signSuccess') {
            $wxsdk.postLbsByGeolocation({
                scene: 7,
                mobile: this.user.mobile,
                userId: this.user.userId
            })
        }
    },
    methods: {
        actionLoanGo() {
            if (window.hybrid && window.hybrid.load) {
                // 帮助 app 跳转页面
                window.hybrid.load(this.selectedConfig.imgType)
            }
            $router.push(this.selectedConfig.path)
        }
    },
    watch: {
        wxReady(newVal) {
            if (newVal == 1 && window.hybrid == null && this.type == 'signSuccess' && this.userLogined) {
                // 签约成功，上传lbs
                $wxsdk.postLbs({
                    scene: 7,
                    mobile: this.user.mobile,
                    userId: this.user.userId
                })
            }
        }
    }
}